<?php
/**
 * Created by PhpStorm.
 * User: lxy
 * Date: 2016/1/14
 * Time: 15:34
 */

namespace app\controllers;

use Yii;
use yii\web\Controller;


use app\models\T_WX_PAY_LOG;

class WeixinPayCallbackController extends Controller
{
    public $enableCsrfValidation = false;

    //卡片回调
    public function actionWxMallCallBack()
    {
        try {


            $echostr = $this->_toXml("0");
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            $data = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

            //写入数据库
            if (YII_DEBUG) {
                file_put_contents('/app/www/wxmall/biz_weixin/runtime/logs/return_code.txt', $postStr);
            }
            if ($data['return_code'] == 'SUCCESS') {
                $err_code_des = '';
                if (array_key_exists('err_code_des', $data))
                    $err_code_des = $data['err_code_des'];
                $attach = '';
                if (array_key_exists('attach', $data))
                    $attach = $data['attach'];
                $out_trade_no=$data['out_trade_no'];
                $order_code=static::getOrder($attach);
                $type=substr($order_code,0,2);
                if($type=='WD') {//card
                    $re = T_WX_PAY_LOG::write_P_WXMALL_PAY_ORDER($data['result_code'], $err_code_des
                        , $data['openid'], $data['total_fee'],$data['transaction_id'], $out_trade_no, $data['time_end']
                        , $attach);
                }
                if($type=='WP') {//bag
                    $re = T_WX_PAY_LOG::P_WXMALL_PAY_DORABAG_ORDER($data['result_code'], $err_code_des
                        , $data['openid'], $data['total_fee'], $out_trade_no,$data['transaction_id'], $data['time_end']
                        , $attach);
                }
                if($type=='SP') {//goods
                    $re = T_WX_PAY_LOG::P_WXMALL_PAY_GOODS($data['result_code'], $err_code_des
                        , $data['openid'], $data['total_fee'], $out_trade_no,$data['transaction_id'], $data['time_end']
                        , $attach);
                }
                if (YII_DEBUG) {

                    file_put_contents('/app/www/wxmall/biz_weixin/runtime/logs/p.txt', '='.$data['result_code'].'-'. $err_code_des
                        .'-'. $data['openid'].'-'. $data['total_fee'].'-'. $out_trade_no.'-'. $data['time_end']
                        .'-'. $attach.'=');
                    file_put_contents('/app/www/wxmall/biz_weixin/runtime/logs/re.txt', $re);
                }
                $echostr = $this->_toXml($re);
            }

            echo $echostr;
        } catch (\Exception $e) {
            Yii:error($e->getMessage());
            if (YII_DEBUG) {
                file_put_contents('/app/www/wxmall/biz_weixin/runtime/logs/error.txt', $e->getMessage());
            }
        }
    }

    private function _toXml($isok)
    {
        $re = 'FAIL';
        if ($isok == '1') {
            $re = 'SUCCESS';
        }
        $res = "<xml>
              <return_code><![CDATA[$re]]></return_code>
              <return_msg><![CDATA[]]></return_msg>
            </xml>";
        return $res;
    }
    public static function getOrder($txt){
//        $txt="current_coin:57;order_no:WD_0000_2016_1988;";
        $data=explode(';',$txt);
        foreach($data as $d){
            $item=explode(':',$d);
            if($item[0]=='order_no'){
                return $item[1];
            }
        }
        return "";
    }

    //测试用的
    public function actionTestBizPay($id)
    {
        if (YII_ENV == 'dev') {
            $re = T_WX_PAY_LOG::write_P_WXMALL_PAY_ORDER('SUCCESS', ''
                , '', '', $id, '', '');
        }
    }

}
